/**
 * Copyright (c) 2004-2006, Martin Fiedler.  All rights reserved.
 * Copyright (c) 2012, Ken Anderson.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 **/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "mcl_image.h"
#include "mcl_visualizer.h"
#include "mcl_common.h"
#include "mcl_timer.h"
#include "mcl_h264_decoder.h"

// Something funny with the SFML-- mcl must be included before these header files.
#include "h264.h"	
#include "perf.h"

//#define USE_X86_ASM

//    "  -z, -zoom <n>    Scale image by n\n"
//    "  -r, -fps <n>     Set maximum frame rate to n fps\n"
//    "      -fps 0       Wait for keypress after each frame\n"
//    "  -f, -frames <n>  Set maximum number of decoded frames\n"
//    "  -l, -loop        Enable looping\n"
//    "  -b, -bench       Benchmark mode\n"
//    "  -p, -perf        Enable performance counting\n"

void do_bench(int maxframe) {
  int frames=0;
  double tdiff;
  mcl::Timer timer;
  
  printf("Benchmarking ...\n");
  timer.start();
  for(frames=0; h264_decode_frame(1); ++frames) {
    if(maxframe && frames>=maxframe) break;
    putchar('\r');
    fflush(stdout);
  }

  tdiff=timer.getSeconds();
  printf("%d frames decoded in %.1f seconds\n= %.1f ms/frame = %.1f fps\n",
         frames,tdiff,1000.0*tdiff/frames,frames/tdiff);
  h264_close();
  perf_summarize();
  exit(0);
}


int main(int argc, char *argv[]) {
  int zoom=1,loop=1,delay=40,bench=0,perf=1,maxframe=0;
  int pause=0,step=0;
  char *filename= INPUT_DIR DDEL "h264" DDEL "manitu.264";

  ///// Initialization ////////////////////////////////////////////////////////

	mcl::Timer timer;
	mcl::H264Decoder decoder;
	decoder.loadFile(filename);
	mcl::Visualizer viz(decoder.getWidth(),decoder.getHeight(),1);
	

  if(perf) perf_enable();
  if(bench) do_bench(maxframe);

  printf("H.264 stream: %dx%d, zooming to %dx%d\n",
         decoder.getWidth(),decoder.getHeight(),
				 decoder.getWidth()*zoom,decoder.getHeight()*zoom);

  ///// Main Loop /////////////////////////////////////////////////////////////

  printf("Hint: ");
  if(delay) printf("[Space] toggles pause; ");
  printf("[Enter] advances to next frame\n");
  pause=(!delay);
  step=1;

  timer.start();
  while(viz.isOpened()) {
    if((!pause) || step) {
      printf("\r");
			if( decoder.decodeAndLoop() == 0 )
			{
				viz.clear();
				viz.handleEvents();
				viz.drawImage(decoder.getImage());
				viz.display();
				fflush(stdout);
			}
			else
			{
				break;
			}
      if(maxframe && h264_frame_no()>=maxframe)
        break;
      if(step) step=0;
    }
  }

  perf_summarize();
  return 0;
}
